package com.stars.services.impl.system;/**
 * Created by luodajie on 2019/1/5.
 */

import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseCode;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.BaseMsg;
import com.stars.common.base.ResultMsg;
import com.stars.common.date.DateUtil;
import com.stars.common.signature.MD5Util;
import com.stars.dao.mapper.system.SystemUserMapper;
import com.stars.dao.mapper.system.SystemUserRelationMapper;
import com.stars.entity.dto.sys.SystemUserAppOrgDTO;
import com.stars.entity.sys.SystemUser;
import com.stars.entity.sys.SystemUserRelation;
import com.stars.services.system.UserConfigService;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author :dj
 * @version : 1.0.0
 * @description
 * @Date 2019-01-05 12:43
 * @note
 */
@Service
public class UserConfigServiceImpl implements UserConfigService {
    @Autowired
    private SystemUserMapper systemUserMapper;
    @Autowired
    private SystemUserRelationMapper systemUserRelationMapper;

    @Override
    public ResultMsg userInsert(SystemUser command, String[] roleIds) throws Exception {
        SystemUserAppOrgDTO su = systemUserMapper.findByName(command.getUserName());
        if (su != null) {
            return ResultMsg.customMsg(BaseCode.USER_EXISTED, BaseMsg.USER_EXISTED, null);
        }

        /** 前端传入的密码已经经过md5加密了。 **/
        String salt = MD5Util.getFixedRandom(6);
        String salt2 = MD5Util.getFixedRandom(6);
        String password = MD5Util.sign(command.getUserPassword(), false, salt, salt2);

        SystemUser systemUser = new SystemUser();
        ObjectCopyUtils.copyPropertiesIf(command,systemUser,BaseConstant.EXCLUDES);
        systemUser.setUserPassword(password);
        systemUser.setUserSalt(salt);
        systemUser.setUserSalt2(salt2);
        systemUser.setUserAddTime(DateUtil.getNewTime());
        systemUserMapper.insertSelective(systemUser);
        //添加角色关系
        addRelation(systemUser.getId(),roleIds);

        return ResultMsg.success();
    }

    @Override
    public ResultMsg userUpdate(SystemUser command, String[] roleIds) throws Exception {
        SystemUser byEntityId = systemUserMapper.selectByPrimaryKey(command.getId());
        ObjectCopyUtils.copyPropertiesIf(command, byEntityId, BaseConstant.UPDATE);
        systemUserMapper.updateByPrimaryKeySelective(byEntityId);
        //1, 根据条件删除对应关系
        systemUserRelationMapper.deleteByUserId(byEntityId.getId());
        //2, 更新用户与角色关系
        addRelation(byEntityId.getId(),roleIds);
        return ResultMsg.success();
    }

    @Override
    public ResultMsg deleteEntity(String ids) throws Exception {
        for (String id : ids.split(BaseConstant.SPOT)) {
            SystemUser systemUser = systemUserMapper.selectByPrimaryKey(id);
            if (systemUser == null) {
                return ResultMsg.customMsg(BaseCode.USER_NOT_EXISTED, BaseMsg.USER_NOT_EXISTED, null);
            }
            systemUserMapper.delete(systemUser);
            //删除用户与角色关系
            systemUserRelationMapper.deleteByUserId(id);
        }

        return ResultMsg.success();
    }

    /**
     * 添加角色关系
     * @param userId
     * @param roleIds
     */
    private void addRelation(String userId, String[] roleIds) {
        for (String id : roleIds) {
            if (StringUtils.isNotBlank(id)) {
                SystemUserRelation systemUserRelation = new SystemUserRelation();
                systemUserRelation.setRelationUserId(userId);
                systemUserRelation.setRelationUserRoleId(id);
                systemUserRelationMapper.insertSelective(systemUserRelation);
            }
        }

    }

    @Override
    public ResultMsg changePassword(SystemUser systemUser) {
        systemUserMapper.updateByPrimaryKeySelective(systemUser);
        return ResultMsg.success();
    }
}
